package com.click.app.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * @author Joel Chuca Condo
 * @date 07/04/2009
 */
@Entity
@Table(name = "laundry_requests")
public class LaundryRequest extends BaseObject {

    private Long id;
    //private Bill bill;
    private Coin coin;
    private Laundry laundry;
    private PeriodPaymentLaundry periodPaymentLaundry;
//    private TradeMark tradeMark;
//    private SizeRank sizeRank;
    private User user;
    private Date date;
    private Double totalPrice;
    private Double failTotalPrice;
    private Date endDate;
    private Boolean payed;    
    private Integer quantity;
    private List<LaundryRequestLaundryPayment> laundryRequestLaundryPayments =
                                                        new ArrayList<LaundryRequestLaundryPayment>();
    private List<LaundryRequestItem> laundryRequestItems = new ArrayList<LaundryRequestItem>();

    private BalanceLaundry balanceLaundry;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id_laundry_request")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_coin")
    public Coin getCoin() {
        return this.coin;
    }

    public void setCoin(Coin coin) {
        this.coin = coin;

    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_laundry", nullable = false)
    public Laundry getLaundry() {
        return laundry;
    }

    public void setLaundry(Laundry laundry) {
        this.laundry = laundry;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_period_payment_laundry", nullable = false)
    public PeriodPaymentLaundry getPeriodPaymentLaundry() {
        return periodPaymentLaundry;
    }

    public void setPeriodPaymentLaundry(PeriodPaymentLaundry periodPaymentLaundry) {
        this.periodPaymentLaundry = periodPaymentLaundry;
    }



    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_user", nullable = false)
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "date", length = 10)
    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    @Column(name = "quantity")
    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    @Column(name = "total_price")
    public Double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    @Column(name = "fail_total_price")
    public Double getFailTotalPrice() {
        return failTotalPrice;
    }

    public void setFailTotalPrice(Double failTotalPrice) {
        this.failTotalPrice = failTotalPrice;
    }

    @OneToOne(cascade = CascadeType.REMOVE, mappedBy = "laundryRequest")
    public BalanceLaundry getBalanceLaundry() {
        return balanceLaundry;
    }

    public void setBalanceLaundry(BalanceLaundry balanceLaundry) {
        this.balanceLaundry = balanceLaundry;
    }

    @Column(name = "payed")
    public Boolean getPayed() {
        return payed;
    }

    public void setPayed(Boolean payed) {
        this.payed = payed;
    }


    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "laundryRequest")
    public List<LaundryRequestLaundryPayment> getLaundryRequestLaundryPayments() {
        return laundryRequestLaundryPayments;
    }

    public void setLaundryRequestLaundryPayments(List<LaundryRequestLaundryPayment> laundryRequestLaundryPayments) {
        this.laundryRequestLaundryPayments = laundryRequestLaundryPayments;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "laundryRequest")
    public List<LaundryRequestItem> getLaundryRequestItems() {
        return laundryRequestItems;
    }

    public void setLaundryRequestItems(List<LaundryRequestItem> laundryRequestItems) {
        this.laundryRequestItems = laundryRequestItems;
    }

    @Transient
    public Double getRealPrice() {
        return totalPrice - failTotalPrice;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof LaundryRequest)) {
            return false;
        }
        LaundryRequest other = (LaundryRequest) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.click.app.model.LaundryRequest[id=" + id + "]";
    }
}
